#include <UnitTest++.h>
#include "../job.h"
#include <iostream>
#include <string>
#include "../option.h"
#include "../MyMap.h"

using namespace std;

struct InputOptionFixture
{
	InputOptionFixture() : temp(22), opt()
	{
	//	cout << "entering Option Fixture" << endl;
		// these strings came from TestThroughPackage.nb don'tforget trailing semicolon
		string sLong ="numchannels 1;samples 50;terminalmode 3;units 0;"
							"messagelevel 0;activeedge 0;setuponly 0;"
							"samplemode 0;transfermech 1;";
		string sDouble= "frequency 100.0;min -10.0;max 10.0;timeout 10.0;";
		string sString = "channels dev1/ai0;clock OnboardClock;";
	
		opt.L = MyMap<long>(sLong);
		opt.D = MyMap<double>(sDouble);
		opt.S = MyMap<string>(sString);
		opt.getNIDAQConstants();
	}
	~InputOptionFixture() { /* cout << "any teardown needed?" << endl;*/}
	
	int temp;
	option opt;
};

TEST(JobClassPassAlways)
{
    CHECK(true);
}

TEST_FIXTURE(InputOptionFixture, JobTestInputOptionFixture)
{
	CHECK_EQUAL(22, temp);
	CHECK_EQUAL(0, opt.L["messagelevel"]);	
	CHECK_EQUAL(opt.NIDAQConstants["terminalmode"][3], opt.L["terminalmode"]);	
//	cout << opt.toString() << endl;
}

TEST_FIXTURE(InputOptionFixture, InputJobReadOneSample)
{
	/* not need to test subclasses directly as Job interface doesn't expose the
	derived classes functions 
	*/
	persistJobHolder persistJobs;
	opt.L["samples"]=1;			//for inputJob one sample
	{
		InputJob* jobptr =0;
		jobptr = new InputJob("single sample",persistJobs,opt); 
		CHECK(jobptr != 0);
		CHECK(jobptr->getTaskHandle() !=0);
		CHECK_EQUAL(1, jobptr->getSampleArraySize());
		jobptr->readData();
		CHECK_CLOSE(0, jobptr->getSampleArray()[0],10); // +/- 10 volts
//		cout << "Show one point: " << jobptr->getSampleArray()[0] << endl;
		delete jobptr;
	}
}

TEST_FIXTURE(InputOptionFixture, BufferedInputJobRead50Points)
{
	/* not need to test subclasses directly as Job interface doesn't expose the
	derived classes functions 
	*/
	persistJobHolder persistJobs;
	{
		BufferedInputJob* jobptr =0;
		jobptr = new BufferedInputJob("single sample",persistJobs,opt); 
		CHECK(jobptr != 0);
		CHECK(jobptr->getTaskHandle() !=0);
		CHECK_EQUAL(opt.L["samples"], jobptr->getSampleArraySize());
		jobptr->readData();
		CHECK_CLOSE(0, jobptr->getSampleArray()[0],10); // +/- 10 volts
//		cout << opt.L["samples"] << " points" << endl;
		for(int i=0; i <50; i++){
//			cout << i << "\t" << jobptr->getSampleArray()[i] << endl;
			CHECK_CLOSE(0, jobptr->getSampleArray()[i],10); // +/- 10 volts
		}
		delete jobptr;
	}
}
